<?php
namespace view;
require_once('time.php');
require_once('cookieEater.php');

class PageView{
	
	private static $username = "username";
	private static $password = "password";
	private static $logOutButton = 'logOutButton';
	private static $logInButton = 'logInButton';
	private static $cookieUsername = 'cookieUsername';
	private static $cookiePassword = 'cookiePassword';
	private static $checkbox = 'checkbox';
	private static $salt = "salt";
	private static $cryptPassword = 'Password';
	
	/*
	 * sets arrays
	 * return array[]
	 * FUL KOD JAJA
	 */
	public function setArray(){
		$array = array();
		
		if(isset($_POST[self::$username])){
			$array[self::$username] = $_POST[self::$username];
		}
		if(isset($_POST[self::$password])){
			$array[self::$password] = $_POST[self::$password];
		}
		if(isset($_COOKIE[self::$username])&& isset($_COOKIE[self::$password])){
			$array[self::$cookieUsername] = $_COOKIE[self::$username];
			$array[self::$cookiePassword] = $_COOKIE[self::$password];
		}
		if(isset($_POST[self::$logOutButton])){
			$array[self::$logOutButton] = $_POST[self::$logOutButton];
		}
		if(isset($_POST[self::$logInButton])){
			$array[self::$logInButton] = $_POST[self::$logInButton];
		}
		if(isset($_POST[self::$checkbox])){
			$array[self::$checkbox] = $_POST[self::$checkbox];
			$this->setCookies();
		}
		return $array;
	}
	
	/* checks if logged in
	 * @return html
	 */
	public function checkIfLoggedIn($message, $sessionLoggedIn){
		
		if($sessionLoggedIn == TRUE){
			$html = $this->loggedInPage($message);
		}else{
			$html = $this->printLoginForm($message);
		}
		return $html;
	}
	
	//@return html
	private function printLoginForm($message = null) {
		
		$this->destroyCookie();
	 	$html = '<!DOCTYPE html>
	 		<head>
	 		<meta charset="utf-8">
	 		<title>Benny är Awesome!</title>
	 		</head>
	 		<body>
	 		<h1>Ej Inloggad!</h1>
	 		';
			$html .= $message;
		$usernameValue = null;
		if (isset($_POST['username'])) {
			$usernameValue = $_POST['username'];	
		}
	 	$html .= 
	 		"<form id='logInForm' action='index.php?attempt' method='post'>
	 		Användarnamn: 
	 		<input type='text' Name='username' id='username' value='$usernameValue' />";		
	 	$html .=	
	 		'Lösenord: 
	 		<input type="password" Name="password" id="password" />
	 		<input type="checkbox" name="checkbox" value="1" id="checkbox"/>Remember Me!
	 		<button type="submit" name="logInButton">Log In</button>
	 		</form>
	 		'. $this->theTime() .'
	 		</body>
	 		</html>';
	 	return $html;
	}
	
	//@return html
	private function loggedInPage($message = null){
		
 		$html = '<!DOCTYPE html>
 		<meta charset="utf-8">
    	<body>';
		
		if ($message != null){
			$html .= $message;
			$message = null;
			}
		
	 	$html .='	
	 		<h1>Admin är inloggad!</h1>
	 		<form action="index.php" method="post">
	 			<button type="buttton" name="logOutButton" 
	 			id="logOutButton">Log Out
	 			</button>
			</form>
			'. $this->theTime() .  $this->eatCookies() .'
	 		</body>
	 		</html>
	 		';
	 
	 	return $html;
	}

	private function setCookies(){
		$cookieTimer = time() + 60;
		file_put_contents("endtime.txt", "$cookieTimer");
		setcookie(self::$username, $_POST[self::$username] ,$cookieTimer);
		self::$cryptPassword = md5($_POST[self::$password] . self::$salt);
		setcookie(self::$password, self::$cryptPassword ,$cookieTimer);
	}

	/*
	 * destroys cookies
	 */
	private function destroyCookie(){
		setcookie('username','',time()-3600);
		setcookie('password','',time()-3600);
	}

	//gets the time
	private function theTime(){
	
		$time = new time();
		return $time->theTime();
	}
	
	//eat cookies
	private function eatCookies(){
		$cookieEater = new cookieEater();		
		return $cookieEater->eatCookies();
	}
}
